package a66weding.com.jiehuntong.adapter;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import org.xutils.x;
import java.util.ArrayList;
import java.util.List;
import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.home.hotel.shop.HomeProductdetailActivity;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.MeCollectBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;
import a66weding.com.jiehuntong.inner.OnItemClickLitener;
/**
 * Created by admin on 2016/8/12.
 */
public class MeCollectActivityAdapter extends BaseRecyclerAdapter<MeCollectActivityAdapter.ViewHolder> {
    //如果有点击事件请设置一个监听;
    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
    private List<MeCollectBean.DataBean> data = new ArrayList<>();
    private Context context;
    public MeCollectActivityAdapter(Context context, List<MeCollectBean.DataBean> data) {
        if (data != null) {
            this.data = data;
        }
        this.context = context;
    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v = LayoutInflater.from(context).inflate(
                R.layout.item_me_indent_collect, parent, false);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }

    public void append(List<MeCollectBean.DataBean> data) {
        if (this.data != null) {
            this.data.addAll(data);
        } else {
            this.data = data;
        }
        notifyDataSetChanged();
    }

    public void clear() {
        if (data != null) {
            this.data.clear();
        }
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position, boolean isItem) {

        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });
        }
        holder.mCodeTv.setText(data.get(position).getNvc_Name());
        holder.mClassifyTv.setText(data.get(position).getProductCategory().getNvc_Name());
        holder.mMoney.setText("¥ " + data.get(position).getDm_ShopPrice());
        x.image().bind(holder.mImageView, data.get(position).getNvc_PicMain());

        holder.mDeleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取商家ID
                int businessId = data.get(position).getI_Id();
                String myUrl = UrlDealUtil.getRealUrlNewIdApi(RequestCode.POST_COLLECTIONCANCEL);
                String url = myUrl + "&productID=" + businessId;
                HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                        new OnNetResultListener() {
                            @Override
                            public void onSucess(SuccessBean sucessbean) {
                                data.remove(data.get(position));
                                notifyDataSetChanged();
                            }

                            @Override
                            public void onError(ErrorBean errorBean) {

                            }
                        }, CommonResultBean.class, RequestCode.POST_COLLECTIONCANCEL);
                HttpHandleUtil.executeCommonPost(context, httpRequestBean);
            }
        });


        holder.mOnclickLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i_id = data.get(position).getI_Id();
                Intent intent = new Intent(context, HomeProductdetailActivity.class);
                intent.putExtra("productId", i_id);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getAdapterItemCount() {
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mImageView;
        private TextView mCodeTv;
        private TextView mClassifyTv;
        private TextView mMoney;
        private Button mDeleteBtn;
        private LinearLayout mOnclickLay;
        private LinearLayout mSwipeLay;

        public ViewHolder(View itemView) {
            super(itemView);
            mImageView = (ImageView) itemView.findViewById(R.id.img_indent);
            mCodeTv = (TextView) itemView.findViewById(R.id.tv_indent_code);
            mClassifyTv = (TextView) itemView.findViewById(R.id.tv_me_classify);
            mMoney = (TextView) itemView.findViewById(R.id.tv_money);
            mDeleteBtn = (Button) itemView.findViewById(R.id.btn_delete);
            mSwipeLay = (LinearLayout) itemView.findViewById(R.id.sw_me_delete);
            mOnclickLay = (LinearLayout) itemView.findViewById(R.id.ll_me_indent_collect);
        }
    }
}
